//
//  RNTPolyvVod.m
//  PolyvVodRnDemo
//
//  Created by 李长杰 on 2019/2/18.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "RNTPolyvVod.h"
#import <PLVVodSDK/PLVVodSDK.h>
#import "PLVVodSkinPlayerController.h"

@interface RNTPolyvVod ()

@property (nonatomic, strong) NSString *vid;
@property (nonatomic, strong) PLVVodSkinPlayerController *player;

@property  (nonatomic, assign) CGRect orginRect;
@property  (nonatomic, assign)  CGRect fullScreenRect;
@property (nonatomic, assign) BOOL fullscreenvalue;

@end

@implementation RNTPolyvVod

- (void)dealloc {
  RCTLog(@"-[%@ %@]",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

// 初始化播放器
- (void)setup {
  self.player = [[PLVVodSkinPlayerController alloc] initWithNibName:nil bundle:nil];
  self.player.rememberLastPosition = NO;
  self.player.enableBackgroundPlayback = YES;
  self.player.autoplay = NO;
  [self addSubview:self.player.view];
  
  CGRect frame = self.player.view.frame;
  frame.size = self.frame.size;
  self.player.view.frame = frame;
  self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  
  self.orginRect = CGRectZero;
  
  CGRect screenBounds = [UIScreen mainScreen].bounds;
  CGFloat screenWidth = screenBounds.size.width;
  CGFloat screenHeight = screenBounds.size.height;
  self.fullScreenRect = CGRectMake(0, 0, screenHeight, screenWidth);
  
  self.player.wrapperView = self;
  
  __weak typeof(self) weakSelf = self;
  [self.player setPlaybackStateHandler:^(PLVVodPlayerViewController *player) {
    RCTLog(@"player setPlaybackStateHandler%ld",(long)player.playbackState);
    if (weakSelf.onPlayVodEvent) {
      weakSelf.onPlayVodEvent(@{@"key":@"paused", @"value": @(player.playbackState != 1)});
    }
  }];
   [self.player setHalfBackClickBlock:^{
     RCTLog(@"player setHalfBackClickBlock");
     if (weakSelf.onPlayVodEvent) {
       weakSelf.onPlayVodEvent(@{@"key":@"goBack", @"value": @"" });
      }
  }];
}

- (void)setPlay_parameters:(NSDictionary *)play_parameters {
  BOOL autoplay = ((NSNumber *)[play_parameters objectForKey:@"is_auto_start"]).boolValue;
  self.player.autoplay = autoplay;
  
  NSString *vid = [play_parameters objectForKey:@"vid"];
  [self updateVid:vid];
  
  BOOL fullScreen = [[play_parameters objectForKey:@"full_screen"] boolValue];
  if (fullScreen) {
    [self performSelector:@selector(setFullScreen) withObject:nil afterDelay:0.1];
  } 
}

// 全屏-半屏切换
- (void)switchToFullScreen:(BOOL)fullScreen {
  RCTLog(@"switchToFullScreen - %@", self);
  if (CGRectEqualToRect(self.orginRect, CGRectZero)) {
    self.orginRect = self.player.view.frame;
  }
  
  if (self.onPlayVodEvent) {
    self.onPlayVodEvent(@{@"key":@"fullscreen", @"value": @(fullScreen)});
  }
  self.fullscreenvalue = fullScreen;
  if (fullScreen) {
    RCTLog(@"switch to fullScreen");
    self.player.view.frame = self.fullScreenRect;
    [self.player.view removeFromSuperview];
    [[UIApplication sharedApplication].keyWindow addSubview:self.player.view];
  } else {
    RCTLog(@"switch to halfScreen");
    if (!CGRectEqualToRect(self.orginRect, CGRectZero)) {
      self.frame = self.orginRect;
      self.player.view.frame = self.orginRect;
      [self.player.view removeFromSuperview];
      [self addSubview:self.player.view];
     
    }
  }
}

// 切换vid
- (void)updateVid:(NSString *)vid {
  if (vid.length == 0 || [_vid isEqualToString:vid]) {
    return;
  }
  _vid = vid;
  __weak typeof(self) weakSelf = self;
  [PLVVodVideo requestVideoWithVid:vid completion:^(PLVVodVideo *video, NSError *error) {
    if (!video.available) {
      return;
    }
    weakSelf.player.video = video;
  }];
}

// 开始/停止
- (void)startOrPause {
  if (self.player.playbackState == PLVVodPlaybackStatePlaying) {
    [self.player pause];
  } else if (self.player.playbackState == PLVVodPlaybackStatePaused
             || self.player.playbackState == PLVVodPlaybackStateStopped) {
    [self.player play];
  }
}

// 开始
- (void)start {
  [self.player play];
}

// 停止
- (void)pause {
  [self.player pause];
}

// 销毁
- (void)destroyPlayer {
  [self.player destroyPlayer];
  self.player = nil;
}

// 横竖屏切换
- (void)setFullScreen {
//  [self setFullScreen:YES];
}

- (void)setFullScreen:(BOOL)fullScreen {
  dispatch_async(dispatch_get_main_queue(), ^{
    if (CGRectEqualToRect(self.player.view.frame, self.fullScreenRect)) {
      if (!fullScreen) {
        [self.player.playerControl.fullShrinkscreenButton sendActionsForControlEvents:UIControlEventTouchUpInside];
      }
    } else {
      if (fullScreen) {
        [self.player.playerControl.fullShrinkscreenButton sendActionsForControlEvents:UIControlEventTouchUpInside];
      }
    }
  });
}

- (void)layoutSubviews
{
  [super layoutSubviews];
  if (!self.fullscreenvalue && CGRectEqualToRect(self.orginRect, self.frame)) {
    self.player.view.frame = self.frame;
  }
    [CATransaction begin];
    [CATransaction setAnimationDuration:0];
    [CATransaction commit];
}
@end

